﻿
#include "complextablewidget.h"
#include "infos.h"

#include <QApplication>
#include<QVector>
#include <QtSql>
#include <QDebug>
int main(int argc, char *argv[])
{
    // 1. 创建并打开数据库连接
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("ZBPZ.db");

    if (db.open()) {
        QSqlQuery query;
#if 0
        // 2.创建数据库表
        QString createTableQuery = "CREATE TABLE IF NOT EXISTS fatable ("
                                   "idFA INTEGER,"
                                   "nameId TEXT,"
                                   "equipId TEXT,"
                                   "equipNum INTEGER"
                                   ");";
        if (query.exec(createTableQuery)) {
            // 3.将数据插入到数据库
            for (const FATable &fa : faTable) {
                QString insertQuery = QString("INSERT INTO fatable (idFA, nameId, equipId, equipNum) "
                                              "VALUES (%1, '%2', '%3', %4);")
                                          .arg(fa.idFA)
                                          .arg(fa.nameId)
                                          .arg(fa.equipId)
                                          .arg(fa.equipNum);
                if (!query.exec(insertQuery)) {
                    qDebug() << "Failed to insert data!";
                }
            }
            // 4.查询数据库中的数据
            QString selectQuery = "SELECT * FROM fatable;";
            if (query.exec(selectQuery)) {
                while (query.next()) {
                    int idFA = query.value("idFA").toInt();
                    QString nameId = query.value("nameId").toString();
                    QString equipId = query.value("equipId").toString();
                    int equipNum = query.value("equipNum").toInt();

                    // 在这里可以对查询到的数据进行处理
                    qDebug() << "idFA: " << idFA;
                    qDebug() << "nameId: " << nameId;
                    qDebug() << "equipId: " << equipId;
                    qDebug() << "equipNum: " << equipNum;
                }
            }else{
                qDebug() << "Failed to execute query!";
            }


        }
        else {
            qDebug() << "Failed to create table!";
        }
#elif 0
        QString createTableQuery = "CREATE TABLE IF NOT EXISTS nTable ("
                                   "name VARCHAR(100),"
                                   "nameId VARCHAR(10),"
                                   "classify INT)";
        if (!query.exec(createTableQuery)) {
            qDebug() << "Error: Failed to create table." << query.lastError();
        }

        // 插入数据
        foreach (const NTable& item, nTable) {
            QString insertQuery = "INSERT INTO nTable (name, nameId, classify) "
                                  "VALUES (:name, :nameId, :classify)";
            query.prepare(insertQuery);
            query.bindValue(":name", item.name);
            query.bindValue(":nameId", item.nameId);
            query.bindValue(":classify", item.classify);
            if (!query.exec()) {
                qDebug() << "Error: Failed to insert data." << query.lastError();
            }
        }
#endif
#if 0
        QString createTableQuery = "CREATE TABLE IF NOT EXISTS cTable ("
                                   "classify INT,"
                                   "className VARCHAR(100))";
        if (!query.exec(createTableQuery)) {
            qDebug() << "Error: Failed to create table." << query.lastError();
        }

        // 插入数据
        foreach (const CTable& item, cTable) {
            QString insertQuery = "INSERT INTO cTable (classify, className) "
                                  "VALUES (:classify, :className)";
            query.prepare(insertQuery);
            query.bindValue(":classify", item.classify);
            query.bindValue(":className", item.className);
            if (!query.exec()) {
                qDebug() << "Error: Failed to insert data." << query.lastError();
            }
        }
#endif

    }else
    {
        qDebug() << "Failed to open database!";
    }

    QApplication a(argc, argv);
    ComplexTableWidget w;
    w.show();
    return a.exec();
}
